﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Autofac;

namespace EditAnyString.Extensions
{
	public static class AutofacExtension
	{
		public static void RegisterEAS( this ContainerBuilder builder,
			Providers.IEASStorageProvider storageProvider,
			Providers.IEASEditAuthorizationProvider editAuthProvider,
			Providers.IEASCacheProvider cacheProvider )
		{
			// and set autofac dependencies (can be used ie in controllers and views)
			if( storageProvider != null )
			{
				builder.Register( c => storageProvider )
							.As<Providers.IEASStorageProvider>()
							.SingleInstance();
			}

			if( editAuthProvider != null )
			{
				builder.Register( c => editAuthProvider )
							.As<Providers.IEASEditAuthorizationProvider>()
							.SingleInstance();
			}

			if( cacheProvider != null )
			{
				builder.Register( c => cacheProvider )
							.As<Providers.IEASCacheProvider>()
							.SingleInstance();
			}

		}

		public static void RegisterEASStorageProvider<EASStorageProviderType>( this ContainerBuilder builder )
			where EASStorageProviderType : Providers.IEASStorageProvider
		{
			builder.RegisterType<EASStorageProviderType>()
						.As<Providers.IEASStorageProvider>()
						.SingleInstance();

		}

		public static void RegisterEASEditAuthenticationProvider<EASEditAuthenticationProviderType>( this ContainerBuilder builder )
			where EASEditAuthenticationProviderType : Providers.IEASEditAuthorizationProvider
		{
			builder.RegisterType<EASEditAuthenticationProviderType>()
						.As<Providers.IEASEditAuthorizationProvider>()
						.SingleInstance();

		}

		public static void RegisterEASCacheProvider<EASCacheProviderType>( this ContainerBuilder builder )
			where EASCacheProviderType : Providers.IEASCacheProvider
		{
			builder.RegisterType<EASCacheProviderType>()
						.As<Providers.IEASCacheProvider>()
						.SingleInstance();

		}
	}
}
